package com.talent.service.iot.mqtt.task;

import com.talent.service.iot.mqtt.model.TlMqttSession;
import com.talent.service.iot.mqtt.model.request.TlMqttPublishReq;
import com.talent.service.iot.mqtt.model.variable.TlMqttPublishVariableHead;
import io.netty.channel.EventLoop;
import io.netty.util.concurrent.ScheduledFuture;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName：RePublishTask
 * @Author: hszhou
 * @Date: 2024/12/24 16:18
 * @Description: 重新投递消息的任务
 */
public class RePublishTask {


    private TlMqttSession session;

    private TlMqttPublishReq req;


    public RePublishTask(TlMqttSession session,TlMqttPublishReq req){
        this.session = session;
        this.req = req;
    }

    public void publish(){

        TlMqttPublishVariableHead variableHead = req.getVariableHead();
        Integer messageId = variableHead.getMessageId();

        //todo 判断消息是否被接收了 如果没有 那么就在这里等待n后再次发送 注意可变头的dup设置为1
        EventLoop eventExecutors = session.getChannel().eventLoop();
        ScheduledFuture<?> schedule = eventExecutors.schedule(() -> {
            this.publish();
        }, 5, TimeUnit.SECONDS);

        //schedule.cancel()

    }
}
